package com.glsc.ngateway.platform.service.cmdb;

import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbDictValue;
import com.glsc.ngateway.common.base.repo.mysql.cmdb.CmdbDictValueRepository;
import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.platform.utils.SpecificationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class CmdbDictValueService {
    @Resource
    private CmdbDictValueRepository dictValueRepository;

    /**
     * 创建实体
     *
     * @param dictValue
     */
    public CmdbDictValue saveDictValue(CmdbDictValue dictValue) {
        return dictValueRepository.save(dictValue);
    }

    /**
     * 查找实体
     *
     * @param id
     * @return
     */
    public CmdbDictValue findById(Integer id) {
        Optional<CmdbDictValue> optional = dictValueRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        } else {
            return null;
        }
    }

    public void deleteById(Integer id) {
        dictValueRepository.deleteById(id);
    }

    /**
     * 分页条件查询
     *
     * @param pageNo
     * @param pageSize
     * @param param
     * @return
     */
    public Page<CmdbDictValue> findPage(int pageNo, int pageSize, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "id";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
        return dictValueRepository.findAll(SpecificationUtil.buildSpecification(param, CmdbDictValue.class), pageable);
    }

    /**
     * 根据条件查询List
     */
    public List<CmdbDictValue> findList(Map<String, Object> param) {
        return dictValueRepository.findAll(SpecificationUtil.buildSpecification(param, CmdbDictValue.class));
    }

    public Page<CmdbDictValue> findPage(int page, int size, String tag) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "id";
        Pageable pageable = PageRequest.of(page - 1, size, direction, sort);
        return dictValueRepository.findAll(new Specification<CmdbDictValue>() {
            @Override
            public Predicate toPredicate(Root<CmdbDictValue> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate p = criteriaBuilder.and(criteriaBuilder.greaterThan(root.get("id").as(Integer.class), 0));
                if (null != tag && tag.length() > 0) {
                    Predicate p1 = criteriaBuilder.like(root.get("label").as(String.class), "%" + tag + "%");
                    Predicate orp = criteriaBuilder.or(p1);
                    p = criteriaBuilder.and(p, orp);
                }
                return p;
            }
        }, pageable);
    }

    public CmdbDictValue findByValueAndDictId(Integer dictId, String value) {
        return dictValueRepository.findByValueAndDictId(dictId, value);
    }

    public List<CmdbDictValue> findDictValueList() {
        return dictValueRepository.findDictValueList();
    }

    /**
     * 字典label和value相似查找
     */
    public List<CmdbDictValue> findByLabelOrValueAndDictCode(String labelOrValue, String dictCode) {
        return dictValueRepository.findByLabelOrValueAndDictCode(labelOrValue, dictCode);
    }

    public List<CmdbDictValue> findByDictCode(String dictCode){
        return dictValueRepository.findByDictCode(dictCode);
    }

    public List<CmdbDictValue> findAllByValueIn(List<String> valueList){
        return dictValueRepository.findAllByValueIn(valueList);
    }
}
